<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="列车编号" prop="trainId">
        <el-input
          v-model="queryParams.trainId"
          placeholder="请输入列车编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车厢号" prop="carriageName">
        <el-input
          v-model="queryParams.carriageName"
          placeholder="请输入车厢号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="座位号" prop="seatNum">
        <el-input
          v-model="queryParams.seatNum"
          placeholder="请输入座位号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="是否删除" prop="isDel">-->
<!--        <el-input-->
<!--          v-model="queryParams.isDel"-->
<!--          placeholder="请输入是否删除"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:seat:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:seat:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:seat:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:seat:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="seatList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="座位id" align="center" prop="seatId" />
      <el-table-column label="列车编号" align="center" prop="trainId" />
      <el-table-column label="车厢号" align="center" prop="carriageName" />
      <el-table-column label="座位号" align="center" prop="seatNum" />
      <el-table-column label="座位状态" align="center" prop="seatStatus" :formatter="seatStatusformatter"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="primary"
              icon="el-icon-s-promotion"
              @click="buySeat(scope.row)"
              v-show="scope.row.seatStatus == 1"
              v-hasPermi="['system:seat:buy']"
          >买票</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:seat:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:seat:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改座位信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="列车编号" prop="trainId">
          <el-input v-model="form.trainId" placeholder="请输入列车编号" />
        </el-form-item>
        <el-form-item label="车厢号" prop="carriageName">
          <el-input v-model="form.carriageName" placeholder="请输入车厢号" />
        </el-form-item>
        <el-form-item label="座位号" prop="seatNum">
          <el-input v-model="form.seatNum" placeholder="请输入座位号" />
        </el-form-item>
        <el-form-item label="状态" prop="isDel">
          <el-radio v-model="form.seatStatus" clearable label="1">可选座</el-radio>
          <el-radio v-model="form.seatStatus" clearable label="2">不可选座</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    买票的窗口   -->
    <el-dialog :title="buytitle" :visible.sync="buyopen" width="500px" append-to-body>
      <el-form ref="form" :model="buyform" :rules="rules" label-width="80px">
        <el-input v-model="buyform.seatId" type="hidden" />
        <el-form-item label="列车编号" prop="trainId">
          <el-input v-model="buyform.trainId" placeholder="请输入列车编号" />
        </el-form-item>
        <el-form-item label="车厢号" prop="carriageName">
          <el-input v-model="buyform.carriageName" placeholder="请输入车厢号" />
        </el-form-item>
        <el-form-item label="座位号" prop="seatNum">
          <el-input v-model="buyform.seatNum" placeholder="请输入座位号" />
        </el-form-item>

        <el-form-item label="发车日期">
<!--          <el-input v-model="buyform.createTime" placeholder="请选择发车日期" />-->
          <el-date-picker
              v-model="buyform.orderStartDate"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="date"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="乘车人" prop="passengerId">
          <el-select v-model="buyform.passengerId" placeholder="请选择乘车人" clearable>
            <el-option
                v-for="p in passengerList"
                :key="p.passengerId"
                :label="p.passengerName"
                :value="p.passengerId"
            />
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitBuyForm">确 定</el-button>
        <el-button @click="buycancel">取 消</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import { listSeat, getSeat, delSeat, addSeat, updateSeat } from "@/api/system/seat";
import { listPassenger} from "@/api/system/passenger";
import { addOrders} from "@/api/system/orders";
import {delLogininfor} from "@/api/monitor/logininfor";

export default {
  name: "Seat",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 座位信息表格数据
      seatList: [],
      // 弹出层标题
      title: "",
      // 买票的弹窗  弹出层标题
      buytitle: "买票",
      // 是否显示弹出层
      open: false,
      // 买票的弹窗 是否显示
      buyopen: false,
      // 乘车人的 list
      passengerList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        trainId: null,
        carriageName: null,
        seatNum: null,
        seatStatus: null,
        isDel: null
      },
      // 表单参数
      form: {},
      // 买票的弹窗 表单参数
      buyform: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    // url上的参数
    var query=this.$route.query;
    // 表单的火车id,直接写死.
    this.form.trainId = query.trainNumber;
    // 查询参数.
    this.queryParams.trainId = query.trainNumber;
    this.getList();
  },
  methods: {
    /** 查询座位信息列表 */
    getList() {
      this.loading = true;
      listSeat(this.queryParams).then(response => {
        this.seatList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取所有的乘车人
    getPassengerList() {
      listPassenger({}).then(response => {
        this.passengerList = response.rows;
        debugger
      });
    },
    // 提交买票的信息
    submitBuyForm() {
      let order = this.buyform;
      // debugger
      addOrders(order).then(response => {
        if (response.code == 200) {
          this.buyopen = false;
          this.$modal.confirm('订单生成成功!!是否跳转到[我的订单]页面去支付?').then(function () {
            window.location.href = "/menu/orders";
          }).catch(() => {});
        } else {
          this.$modal.msgSuccess(response.msg);
        }
      });
    },
    seatStatusformatter(row, column, cellValue, index){
      if(cellValue == '1') return '可选座';
      if(cellValue == '2') return '不可选座';
      return '-';
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 买票 取消按钮
    buycancel() {
      this.buyopen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        seatId: null,
        trainId: null,
        carriageName: null,
        seatNum: null,
        seatStatus: "0",
        isDel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      debugger
      // this.resetForm("queryForm");
      // this.queryParams.trainId = null;
      // this.queryForm.trainId = null;
      // this.handleQuery();
      // 上面重置方式,不行,没有效果.所以直接让页面转到这个页面.并且不带有参数.
      window.location.href="/menu/seat"
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.seatId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加座位信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const seatId = row.seatId || this.ids
      getSeat(seatId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改座位信息";
      });
    },
    buySeat(row) {
      this.reset();
      const seatId = row.seatId || this.ids
      // 弹窗: 选择火车(可自动带过来)、选择座位(可自动带过来)、选择乘客下拉
      debugger
      getSeat(seatId).then(response => {
        this.buyform = response.data;
        this.buyopen = true;
        debugger
      });
      // 查询所有的乘车人列表
      this.getPassengerList();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.seatId != null) {
            updateSeat(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSeat(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const seatIds = row.seatId || this.ids;
      this.$modal.confirm('是否确认删除座位信息编号为"' + seatIds + '"的数据项？').then(function() {
        return delSeat(seatIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/seat/export', {
        ...this.queryParams
      }, `seat_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
